package com.sumer.psychology.friendship.resource;

import com.sumer.psychology.common.application.ApplicationServiceRegistry;
import com.sumer.psychology.common.domain.model.User;
import com.sumer.psychology.common.resource.AbstractResource;
import com.sumer.psychology.friend.application.command.ConfirmFriendshipCommand;
import com.sumer.psychology.friend.application.command.DeleteFriendshipCommand;
import com.sumer.psychology.friend.application.command.RejectFriendCommand;
import com.sumer.psychology.friend.application.command.RequestFriendCommand;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.Set;

/**
 * 朋友关系的rest接口
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/9/25
 * Time: 9:44
 */
@Path("/v1/friendship/")
@Consumes(MediaType.APPLICATION_JSON)
public class FriendshipResource extends AbstractResource {
    @Context
    private UriInfo uriInfo;

    @POST
    public Response requestFriend(RequestFriendCommand aRequestFriendCommand) {
        ApplicationServiceRegistry
                .friendshipApplicationService()
                .requestFriend(aRequestFriendCommand);

        return Response.status(Response.Status.ACCEPTED).build();
    }

    @PUT
    public Response rejectFriend(RejectFriendCommand aRejectFriendCommand) {
        ApplicationServiceRegistry
                .friendshipApplicationService()
                .rejectFriend(aRejectFriendCommand);

        return Response.status(Response.Status.ACCEPTED).build();
    }

    @PUT
    @Path("requester/{requesterId}/acceptor/{acceptorId}")
    public Response confirmFriend(
            @PathParam("requesterId") String requesterId,
            @PathParam("acceptorId") String acceptorId,
            ConfirmFriendshipCommand aConfirmFriendshipCommand) {
        ApplicationServiceRegistry
                .friendshipApplicationService()
                .confirmFriendship(new ConfirmFriendshipCommand(
                        requesterId,
                        acceptorId,
                        aConfirmFriendshipCommand.messageId()));

        return Response.status(Response.Status.ACCEPTED).build();
    }

    @DELETE
    @Path("requester/{requesterId}/acceptor/{acceptorId}")
    public Response deleteFriend(
            @PathParam("requesterId") String requesterId,
            @PathParam("acceptorId") String acceptorId) {
        ApplicationServiceRegistry
                .friendshipApplicationService()
                .deleteFriendship(new DeleteFriendshipCommand(
                        requesterId,
                        acceptorId));

        return Response.status(Response.Status.ACCEPTED).build();
    }

    @GET
    @Path("{userId}/friends")
    public Response getFriendsByUserId(@PathParam("userId") String userId) {
        Set<User> friends = ApplicationServiceRegistry
                .friendshipQueryApplicationService()
                .friendsOfMemberId(userId);

        return Response.ok(friends).build();
    }
}
